from django.http import JsonResponse
from functools import wraps


def is_authenticated(func):
    @wraps(func)
    def wrapper(me_instance, request, *args, **kwargs):
        if not request.user or not request.user.is_authenticated:
            return JsonResponse({'flag': False, 'code': 80000, 'errorMsg': '未登录'}, status=508)
        return func(me_instance, request, *args, **kwargs)
    return wrapper


def is_superuser(func):
    @wraps(func)
    def wrapper(me_instance, request, *args, **kwargs):
        if not request.user or not request.user.is_authenticated:
            return JsonResponse({'flag': False, 'code': 80000, 'errorMsg': '未登录'}, status=508)
        else:
            if not request.user.is_superuser:
                return JsonResponse({'flag': False, 'code': 80808, 'errorMsg': '没有权限'}, status=508)

        return func(me_instance, request, *args, **kwargs)
    return wrapper
